﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Restaurant.DAO;
using Restaurant.DTO;

namespace Restaurant.BUS
{
    public class NhaCungCapService
    {
        private NhaCungCapDB nccDAO = new NhaCungCapDB();

        public DataTable LoadTatCaNhaCungCap()
        {
            return nccDAO.LoadTatCaNhaCungCap();
        }

        public DataTable LoadHanThanhToan()
        {
            return nccDAO.LoadHanThanhToan();
        }

        public bool ThemNhaCungCap(NhaCungCap ncc)
        {
            if (ncc.TenNCC == "" || ncc.Diachi == "" || ncc.Sdt == "")
            {
                return false;
            }
            if (nccDAO.ThemNhaCungCap(ncc.TenNCC, ncc.Diachi, ncc.Sdt, ncc.MaHTT))
                return true;
            return false;
        }

        public bool SuaNCC(NhaCungCap ncc)
        {
            if (nccDAO.SuaNhaCungCap(ncc.MaNCC, ncc.TenNCC, ncc.Diachi, ncc.Sdt, ncc.MaHTT))
                return true;
            return false;
        }

        public bool XoaNCC(int ma)
        {
            if (nccDAO.XoaNhaCungCapID(ma))
                return true;
            return false;
        }

        public NhaCungCap TimNCCTen(string ten)
        {
            NhaCungCap ncc = new NhaCungCap();
            DataTable dbtab = nccDAO.TimNCCTen(ten);
            DataRow dbrow = dbtab.Rows[0];
            ncc.MaNCC = int.Parse(dbrow[0].ToString());
            ncc.TenNCC = dbrow[1].ToString();
            ncc.Diachi = dbrow[2].ToString();
            ncc.Sdt = dbrow[3].ToString();
            ncc.MaHTT = int.Parse(dbrow[4].ToString());
            return ncc;
        }

        public NhaCungCap TimNCCID(int ma)
        {
            NhaCungCap ncc = new NhaCungCap();
            DataTable dbtab = nccDAO.TimNCCID(ma);
            DataRow dbrow = dbtab.Rows[0];
            ncc.MaNCC = int.Parse(dbrow[0].ToString());
            ncc.TenNCC = dbrow[1].ToString();
            ncc.Diachi = dbrow[2].ToString();
            ncc.Sdt = dbrow[3].ToString();
            ncc.MaHTT = int.Parse(dbrow[4].ToString());
            return ncc;
        }

        public int TimMaHTTTheoTen(string ten)
        {
            DataTable dbtab = nccDAO.TimHanTTTheoTen(ten);
            return int.Parse(dbtab.Rows[0][0].ToString());
        }

        public string TimMaHTTTheoMa(int ma)
        {
            DataTable dbtab = nccDAO.TimHanTTTheoMa(ma);
            DataRow dbrow = dbtab.Rows[0];
            return dbrow[1].ToString();
        }

        public DataTable LoadNCCDirtyreadError()
        {
            return nccDAO.LoadNCCDirtyreadError();
        }

        public DataTable LoadNCCDirtyread()
        {
            return nccDAO.LoadNCCDirtyread();
        }

        public bool SuaNCCDirtyreadError(NhaCungCap ncc)
        {
            if(ncc.MaNCC.ToString()==""|| ncc.TenNCC==""|| ncc.Diachi==""|| ncc.Sdt==""|| ncc.MaHTT.ToString()=="")
                return false;
            if (nccDAO.SuaNCCDirtyreadError(ncc.MaNCC, ncc.TenNCC, ncc.Diachi, ncc.Sdt, ncc.MaHTT))
                return true;
            return false;
        }

        public bool SuaNCCDirtyread(NhaCungCap ncc)
        {
            if (ncc.MaNCC.ToString() == "" || ncc.TenNCC == "" || ncc.Diachi == "" || ncc.Sdt == "" || ncc.MaHTT.ToString() == "")
                return false;
            if (nccDAO.SuaNCCDirtyread(ncc.MaNCC, ncc.TenNCC, ncc.Diachi, ncc.Sdt, ncc.MaHTT))
                return true;
            return false;
        }

        public DataTable LoadNCCUnrepeatError(ref int soluong)
        {
            return nccDAO.LoadNCCUnrepeatError(ref soluong);
        }

        public DataTable LoadNCCUnrepeat(ref int soluong)
        {
            return nccDAO.LoadNCCUnrepeat(ref soluong);
        }

        public DataTable LoadNCCPhantomError(ref int soluong)
        {
            return nccDAO.LoadNCCPhantomError(ref soluong);
        }

        public DataTable LoadNCCPhantom(ref int soluong)
        {
            return nccDAO.LoadNCCPhantom(ref soluong);
        }
    }
}
